package collection.map;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * @author jiangwz
 * @create 2020/11/12.
 */
public class MyTests {
    static final ConcurrentMap<String, String> db = new ConcurrentHashMap<>();

    static void initDb() {
        db.put("11111", "11111-val");
    }

    public static void main(String[] args) {
        initDb();

        /**
         * 没有就存,有就返回原值
         */
//        testPutIfAbsent();

        /**
         * 有就返回val,没有就执行function (key) -> loadToDB(key);
         * 也就是说,当key不在的时候就执行函数
         */
//        testComputeIfAbsent();

        /**
         * 不管有没有key对应的value,最终都会compute一个value
         *
         * 也就是说 里面的函数一定执行
         */
        testCompute();

        /**
         * 当key存在时,执行函数
         */
//        testComputeIfPresent();

    }

    private static void testComputeIfPresent() {
        String key = "1111";
        ConcurrentMap<String, String> map = new ConcurrentHashMap<>();
        map.put(key, "33");
        System.out.println(map);
        String computeIfPresent = map.computeIfPresent(key, (k, v) -> {
            System.out.println(k+"-"+v);
            return "1";
        });
        System.out.println(computeIfPresent);
        System.out.println(map);

    }

    /**
     * 如果key有值, compute后 key对应的值是compute后的新值
     * 如果key无值,则 val= compute后的新值
     *
     */
    private static void testCompute() {
        //创建一个 HashMap
        HashMap<String, Integer> prices = new HashMap<>();

        // 往HashMap中添加映射项
//        prices.put("Shoes", 200);
        prices.put("Bag", 300);
        prices.put("Pant", 150);
        System.out.println("HashMap: " + prices);

        // 重新计算鞋子打了10%折扣后的值
        int newPrice = prices.compute("Shoes", (key, value) -> value == null ? 200 : value - value * 10/100);
        System.out.println("Discounted Price of Shoes: " + newPrice);

        // 输出更新后的HashMap
        System.out.println("Updated HashMap: " + prices);
    }

    private static void testComputeIfAbsent() {
        ConcurrentMap<String, String> map = new ConcurrentHashMap<>();
        map.put("11111", "33");
        System.out.println(map);
        String s = map.computeIfAbsent("11111", key -> {
            return db.getOrDefault(key, "666");
        });
        System.out.println(s);
        System.out.println(map);
    }

    /**
     * if (!map.containsKey(key))
     * return map.put(key, value);
     * else
     * return map.get(key);
     */
    public static void testPutIfAbsent() {
        ConcurrentMap<String, List<String>> map = new ConcurrentHashMap<>();
        List<String> notes = Collections.synchronizedList(new ArrayList<String>());
        notes.add("titan");
        List<String> list = map.putIfAbsent("112", notes);
        System.out.println(list);
        System.out.println(map);

        List<String> list1 = map.putIfAbsent("112", Collections.synchronizedList(new ArrayList<String>()));
        System.out.println(list1);
        System.out.println(map);

        ConcurrentMap<String, String> map2 = new ConcurrentHashMap<>();
        String s = map2.putIfAbsent("11", "22");
        System.out.println(s);
        String s1 = map2.putIfAbsent("11", "33");
        System.out.println(s1);
        System.out.println(map2);
    }
}
